package com.ruoyi.tracks.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.tracks.domain.Tracks;
import com.ruoyi.tracks.service.ITracksService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 歌曲Controller
 *
 * @author lin
 * @date 2025-03-04
 */
@RestController
@RequestMapping("/tracks/tracks")
public class TracksController extends BaseController
{
    @Autowired
    private ITracksService tracksService;

    /**
     * 查询歌曲列表
     */
    @PreAuthorize("@ss.hasPermi('tracks:tracks:list')")
    @GetMapping("/list")
    public TableDataInfo list(Tracks tracks)
    {
        startPage();
        List<Tracks> list = tracksService.selectTracksList(tracks);
        return getDataTable(list);
    }

    /**
     * 导出歌曲列表
     */
    @PreAuthorize("@ss.hasPermi('tracks:tracks:export')")
    @Log(title = "歌曲", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Tracks tracks)
    {
        List<Tracks> list = tracksService.selectTracksList(tracks);
        ExcelUtil<Tracks> util = new ExcelUtil<Tracks>(Tracks.class);
        util.exportExcel(response, list, "歌曲数据");
    }

    /**
     * 获取歌曲详细信息
     */
    @PreAuthorize("@ss.hasPermi('tracks:tracks:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tracksService.selectTracksById(id));
    }

    /**
     * 新增歌曲
     */
    @PreAuthorize("@ss.hasPermi('tracks:tracks:add')")
    @Log(title = "歌曲", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Tracks tracks)
    {
        return toAjax(tracksService.insertTracks(tracks));
    }

    /**
     * 修改歌曲
     */
    @PreAuthorize("@ss.hasPermi('tracks:tracks:edit')")
    @Log(title = "歌曲", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Tracks tracks)
    {
        return toAjax(tracksService.updateTracks(tracks));
    }

    /**
     * 删除歌曲
     */
    @PreAuthorize("@ss.hasPermi('tracks:tracks:remove')")
    @Log(title = "歌曲", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tracksService.deleteTracksByIds(ids));
    }

    /**
     * 根据歌手ID获取歌单中所有歌曲的歌名
     */
    @PreAuthorize("@ss.hasPermi('tracks:tracks:personquery')")
    @GetMapping("/trackDetails/{personId}")
    public AjaxResult getTrackDetails(@PathVariable("personId") Long personId) {
        List<Map<String, Object>> trackDetails = tracksService.selectTrackDetailsByPersonId(personId);
        return success(trackDetails);
    }

    @PreAuthorize("@ss.hasPermi('tracks:tracks:lovequery')")
    @GetMapping("/loveDetails/{userId}")
    public AjaxResult getLoveDetails(@PathVariable("userId") Long userId) {
        List<Map<String, Object>> loveDetails = tracksService.selectTrackDetailsByUserId(userId);
        return success(loveDetails);
    }

    @PreAuthorize("@ss.hasPermi('tracks:tracks:recommend')")
    @GetMapping("/recommend/{userId}")
    public AjaxResult getRecommendDetails(@PathVariable("userId") Long userId) {
        List<Map<String, Object>> recommendDetails = tracksService.recommendTrackDetailsByUserId(userId);
        return success(recommendDetails);
    }
}
